// pages/doctor-case-add-3/doctor-case-add-3.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl,
    goodslist: [],
    pageSize: 5,
    pageNum: 1,
    choosedId: '',
    oneTypeId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    this.setData({
      choosedId: options.goodsId||'',
      oneTypeId: options.choosedid
    });
    this.loadData();
  },

  // 加载医美产品数据
  loadData: function(more) {
    var that = this;
    app.request({
      url: 'doctor/product/getPage',
      data: {
        'pageSize': that.data.pageSize,
        'pageNum': that.data.pageNum,
        'doctorId': wx.getStorageSync('userid'),
        'oneTypeId': that.data.oneTypeId
      },
      success(res) {
        wx.hideLoading();
        if (more) {
          if (res.data.length) {
            var goodlist = that.data.goodslist.concat(res.data);
            that.setData({
              'goodslist': goodlist,
            })
          } else {
            wx.showToast({
              title: '已无更多...',
              icon: 'none',
              duration: 1000,
            });
          }
        } else {
          that.setData({
            'goodslist': res.data,
          })
        }
      }
    })
  },

  // 点击之后返回上一页
  goBackLastPage: function(e) {
    let id = e.currentTarget.dataset.id;
    let info = e.currentTarget.dataset.content;
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2]; //上一个页面
    if (id == this.data.choosedId){
      //取消
      this.setData({
        choosedId: ''
      });
      prevPage.setData({
        'goodsid': '',
        'caseinfo': [],
        'hasChoosed': 2,
      });
    }
    else{
      //选中
      this.setData({
        choosedId: id
      });
      prevPage.setData({
        'goodsid': id,
        'caseinfo': info,
        'hasChoosed': 1,
      });
    }
    wx.navigateBack({
      delta: 1,
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    wx.showLoading({
      title: '加载中...',
    })
    this.setData({
      'pageNum': this.data.pageNum + 1,
    })
    this.loadData(1);
  }
})